package com.lollipop.chain.simplechain;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: J.K
 * @Date: 2021-08-31 13:40
 * @Description: 构建过滤器链
 */
public class FilterChain implements Filter {

    List<Filter> filters = new ArrayList<>();

    /**
     * 添加过滤器
     * 支持链式添加的方法：返回FilterChain本身
     *
     * @param filter
     * @return
     */
    public FilterChain addFilter(Filter filter) {
        this.filters.add(filter);
        return this;
    }

    @Override
    public String doFilter(String str) {
        String r = str;
        for (Filter f : filters) {
            r = f.doFilter(r);
        }
        return r;
    }
}
